package cn.autumnorange.app.user.consumer.authorization.security.security;

import cn.autumnorange.app.common.dto.ResultDto;
import cn.autumnorange.app.common.rpc.RPCService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginPageController {
  @Value("${server.port}")
  private int port;

  @GetMapping("loginPage")
  @ResponseBody
  public ResponseEntity loginPage(HttpServletRequest httpServletRequest) {
    System.out.println(
        "loginPage:" + port + "  rpcId" + httpServletRequest.getHeader(RPCService.RPCID));
    //    try {
    //      Thread.sleep(8400);
    //    } catch (InterruptedException e) {
    //      e.printStackTrace();
    //    }
    Enumeration e = httpServletRequest.getParameterNames();
    Map<Object, Object> map = new HashMap<>();
    while (e.hasMoreElements()) {
      Object paramName = e.nextElement();
      map.put(paramName, httpServletRequest.getParameter(paramName.toString()));
    }
    Enumeration<String> enumerations = httpServletRequest.getHeaderNames();
    if (enumerations != null) {
      while (enumerations.hasMoreElements()) {
        String var = enumerations.nextElement();
        map.put(var, httpServletRequest.getHeader(var));
        if(var.equalsIgnoreCase("cookie")){
          map.put("authcookie", httpServletRequest.getHeader(var));
        }
      }
    }
    Cookie[] cookies= httpServletRequest.getCookies();

    if (cookies != null) {
      for (Cookie c : cookies) {

        System.out.println(c.getName() + "--->" + c.getValue());
        map.put(c.getName(),c.getValue());
      }


    }else{
      System.out.println("登录校验过滤器AjaxJsonUsernamePasswordAuthenticationFilter获取cookie null");

    }
    map.put("test","test2222222222222222222222222222");
    return ResponseEntity.ok(ResultDto.createDataResult(map));
  }
}
